package com.example.smartcity.Controller;

import com.alibaba.fastjson.JSONObject;
import com.example.smartcity.Service.Impl.FileUploadService;
import com.example.smartcity.Service.Impl.SSPServiceImpl;
import com.example.smartcity.Service.SSPService;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.example.smartcity.Service.Impl.FileUploadService;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.Type;
import java.sql.SQLException;
import java.util.List;

@RestController
@RequestMapping("/suiShouPai")
public class SuiShouPaiController {
    private final FileUploadService fileUploadService;
    private final SSPServiceImpl sspService;
    public SuiShouPaiController(FileUploadService fileUploadService, SSPServiceImpl sspService){
        this.fileUploadService = fileUploadService;
        this.sspService = sspService;
    }
    @PostMapping("/upload")
    public String upload(@RequestParam("files") String files,
                         @RequestParam("names") String names,
                         @RequestParam("content") String content,
                         @RequestParam("title") String title,
                         @RequestParam("uid")String uid) throws SQLException, IOException {
        // 在这里处理上传的文件，例如保存到数据库或存储服务
        Type fileType = new TypeToken<List<byte[]>>() {}.getType();
        Gson gson = new Gson();
        List<byte[]> imgs;
        imgs = gson.fromJson(files, fileType);
        List<String> imgnames = gson.fromJson(names, new TypeToken<List<String>>() {}.getType());
        String textcontent = gson.fromJson(content,String.class);
        String texttitle = gson.fromJson(title,String.class);
        fileUploadService.savesuishoupaiimg(imgs,imgnames,textcontent,texttitle,uid);
        JSONObject map = new JSONObject();
        map.put("message", "文件上传成功");
        map.put("code","200");
        return map.toJSONString();

    }
    @RequestMapping("/download")
    public String download(HttpServletResponse response) throws IOException {
        String result = sspService.findAll();
        return result;
    }
}